﻿'''
LCR122 路径加密
假定一段路径记作字符串path，其中以"."作为分隔符。现需将路径加密，加密方法为将path中的分隔符替换为空格" "，
请返回加密后的字符串。
示例 1：
输入：path = "a.aef.qerf.bb"
输出："a aef qerf bb"
限制：
0 <= path.length <= 10000
扩展：'.'替换为多个字符'20%'
'''
class Solution(object):
    '''
    '.'替换为空格或单个字符
    def pathEncryption(self,path:str)->str:
        result = list(path)
        right = len(result) - 1
        while right >= 0:
            if result[right] == '.':
                result[right] = ' '
            right -= 1
        return ''.join(result)
    '''
    #'.'替换为多个字符（20%）
    def pathEncryption(self,path:str)-> str:
        counter = path.count('.')
        result = list(path)
        result.extend('.' * counter * 2)
        left, right = len(path) - 1, len(result) - 1
        while left >= 0:
            if path[left] != '.':
                result[right] = path[left]
                right -= 1
            else:
                result[right - 2:right + 1] = '20%'
                right -= 3
            left -= 1
        return ''.join(result)        
#示例
if __name__ == '__main__':
    path = "a.aef.qerf.bb"
    print(Solution().pathEncryption(path))



